package Week10;

import java.util.ArrayList;

public class Polygon extends PolyLine {
	//A polygon is defined with an ArrayList of Points.
	//A polygon has many similarities with a polyline.
	//However, we check that the first and last coordinate in the list are the same.


	
	public Polygon(String label){
		super(label);
	}
	
	public boolean isPolygon(Polygon plg){
		if (
				plg.getPolypoints().get(0).getPt().getX() == plg.getPolypoints().get(plg.getPolypoints().size()-1).getPt().getX() &&
				plg.getPolypoints().get(0).getPt().getY() == plg.getPolypoints().get(plg.getPolypoints().size()-1).getPt().getY()
				)
			return true;
		else {
			return false;
		}
	}

	
	
	public Double computeLength(Polygon plg) {
		if (plg.isPolygon(plg)){
			double totalLength = 0, segmentLength = 0;

			for(int i=0; i<polypoints.size()-1; i++){
	        	segmentLength = 
	        			Math.pow(
	        					(
	        						( Math.pow(polypoints.get(i).getPt().getX() - polypoints.get(i+1).getPt().getX(), 2) ) +
	        						( Math.pow(polypoints.get(i).getPt().getY() - polypoints.get(i+1).getPt().getY(), 2) )
	        						)
	        					, 0.5);
	        	totalLength = totalLength + segmentLength;
	        }
	
			return totalLength;
		}
		else {
			System.out.println("This set of points is not a polygon. There is no polygon length ...");
			return null; // Returning zero should be avoided for "no length"
		}
	}
}


